//
//  AISBLEDevice.m
//  ParkingCloud
//
//  Created by Ares on 16/5/9.
//  Copyright © 2016年 NJSubier. All rights reserved.
//

#import "AISBLEDevice.h"

@interface AISBLEDevice() <CBPeripheralDelegate> {
    
}

@end

@implementation AISBLEDevice

+ (CBUUID *)serviceUUID {
    return nil;
}

#pragma mark Property

- (NSString *)deviceName {
    if (nil == self.peripheral) {
        return nil;
    }
    return self.peripheral.name;
}

- (void)setPeripheral:(CBPeripheral *)peripheral {
    _peripheral = peripheral;
    [peripheral setDelegate:self];
}

#pragma mark Function will invoked by Manager

- (void)deviceConnectedFailedWithError:(nullable NSError *)error {
    
}

- (void)deviceDisconnectedWithError:(nullable NSError *)error {
    
}

- (nullable NSArray<CBUUID *> *)interestedServiceUUIDs {
    return nil;
}

- (nullable NSArray<CBUUID *> *)characteristicUUIDsForService:(nonnull CBService *)service {
    return nil;
}

- (void)setupDeviceWithDiscoveredCharacteristics:(nullable NSArray<CBCharacteristic *> *)characteristics {
}

- (void)valueUpdatedForCharacteristic:(nullable CBCharacteristic *)characteristic {
}

- (void)valueWrittenForCharacteristic:(nullable CBCharacteristic *)characteristic {
    
}

- (void)notificationStateUpdatedForCharacteristic:(nullable CBCharacteristic *)characteristic {
    
}

#pragma mark CBPeripheralDelegate

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error {
    if (error != nil) {
        NSLog(@"DiscoverServices Error:\n%@", error.description);
        return ;
    }
    
    if (peripheral != self.peripheral) {
        return ;
    }
    
    NSArray *services = nil;
    services = [peripheral services];
    if (! services || ! [services count]) {
        return ;
    }
    
    for (CBService *service in services)
    {
        NSArray<CBUUID *> *serviceUUIDs = [self interestedServiceUUIDs];
        if ([serviceUUIDs count] > 0 && ! [serviceUUIDs containsObject:service.UUID]) {
            continue;
        }
        
        NSArray<CBUUID *> *characteristicUUIDs = [self characteristicUUIDsForService:service];
        [self.peripheral discoverCharacteristics:characteristicUUIDs forService:service];
    }
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error {
    if (error != nil) {
        NSLog(@"DiscoverCharacteristics Error:\n%@", error.description);
        return ;
    }
    
    if (peripheral != self.peripheral) {
        return ;
    }
    
    NSArray<CBCharacteristic *> *characteristics = [service characteristics];
    [self setupDeviceWithDiscoveredCharacteristics:characteristics];
}

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    if (nil != error) {
        NSLog(@"UpdateValueForCharacteristic Error:\n%@", error.description);
        return ;
    }
    
    if (peripheral != self.peripheral) {
        return ;
    }
    
    [self valueUpdatedForCharacteristic:characteristic];
}

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    if (nil != error) {
        NSLog(@"WriteValueForCharacteristic Error:\n%@", error.description);
        return ;
    }
    
    if (peripheral != self.peripheral) {
        return ;
    }
    
    [self valueWrittenForCharacteristic:characteristic];
}

- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    if (nil != error) {
        NSLog(@"UpdateNotificationState Error:\n%@", error.description);
        return ;
    }
    
    if (peripheral != self.peripheral) {
        return ;
    }
    
    [self notificationStateUpdatedForCharacteristic:characteristic];
}

@end
